package com.wangp.mywrite.s_juc.vol;


import java.util.concurrent.CountDownLatch;

/**
 * @description: 指令重排证明
 * @author tooStronger
 * @date 2023/2/11
 */
public class InstructionReset {

    static int a;
    static int b;
    static int x;
    static int y;

    public static void main(String[] args) throws InterruptedException {
        for (long i = 0; i < Long.MAX_VALUE; i++) {
             a = 0;
             b = 0;
             x = 0;
             y = 0;

            CountDownLatch latch = new CountDownLatch(2);

            Thread t1 = new Thread(() -> {
                a = 1;
                x = b;
                latch.countDown();
            });

            Thread t2 = new Thread(() -> {
                b = 1;
                y = a;
                latch.countDown();
            });

            t1.start();
            t2.start();
            latch.await();

            String result = "第" + i + "次 {" + x + "," + y  + "}";
            if(x  == 0 && y== 0){
                System.out.println(result);
                break;
            }
        }

    }
}
